package com.fancy.mobileschool.service;


import android.text.LoginFilter;
import android.util.Log;

import com.fancy.mobileschool.entity.Friend;
import com.fancy.mobileschool.entity.PersonChatMessage;
import com.fancy.mobileschool.util.OKHttpUtil;
import com.fancy.mobileschool.util.GsonUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Jackie on 2017/10/21.
 */

public class ChatFriendsService {

    private OkHttpClient httpClient = OKHttpUtil.getHttpClient();

    private Gson gson = GsonUtil.getGson();

    private static final String yun =  "http://101.200.35.107:8080/MobileSchool-chat/person_chat";

    private static final String baseUrl = "http://192.168.31.224:8081/person_chat";

    public List<Friend> getMyFriends(String myID){
        List<Friend> rs = null;
        try {
            Request request = new Request.Builder().url(baseUrl + "/getMyFriends.do?myID=" + myID).build();
            Response response = httpClient.newCall(request).execute();
            Type type = new TypeToken<ArrayList<Friend>>(){}.getType();
            rs=gson.fromJson(response.body().string(), type);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rs;
    }

    public List<PersonChatMessage> getMyConversation(String myID, String otherID) {

        List<PersonChatMessage> rs = null;
        try {
            Request request = new Request.Builder().url(baseUrl + "/getConversation.do?myID=" + myID + "&otherID=" + otherID).build();
            Response response = httpClient.newCall(request).execute();

            if (response.code() == 200){
                Type type = new TypeToken<ArrayList<PersonChatMessage>>(){}.getType();
                rs = gson.fromJson(response.body().string(), type);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return rs;
    }

    public PersonChatMessage sendMessage(PersonChatMessage personChatMessage) {
        try {
            FormBody body = new FormBody.Builder()
                    .add("body", gson.toJson(personChatMessage)).build();
            Request request = new Request.Builder().url(baseUrl + "/sendMessage.do").post(body).build();
            Response response = httpClient.newCall(request).execute();
            if (response.code() == 200&&response.body().string().equals("{\"body\":\"OK\"}")){
                return personChatMessage;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<PersonChatMessage> getNotReadMessage(String myID, String otherID){
        List<PersonChatMessage> rs = null;
        try {
            Request request = new Request.Builder().url(baseUrl + "/getNotReadMessage.do?myID=" + myID + "&otherID=" + otherID).build();
            Response response = httpClient.newCall(request).execute();
            if (response.code() == 200){
                Type type = new TypeToken<ArrayList<PersonChatMessage>>(){}.getType();
                rs = gson.fromJson(response.body().string(), type);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rs;
    }

    public Friend findFriend(String friendID) {
        Friend friend = null;
        try {
            Request request = new Request.Builder().url(baseUrl + "/findFriends.do?friendID=" + friendID ).build();
            Response response = httpClient.newCall(request).execute();
            if (response.code() == 200){
                Type type = new TypeToken<Friend>(){}.getType();
                friend = gson.fromJson(response.body().string(), type);
            }
        }catch (Exception e){

        }
        return friend;

    }

    public void addFriend(String myID, String friendID) {
        try {
            Request request = new Request.Builder().url(baseUrl + "/addFriends.do?person_1=" + myID + "&person_2=" + friendID ).build();
            Response response = httpClient.newCall(request).execute();
            if (response.code() == 200) {
                Log.i("wode","ceee");
            }else {
                Log.i("wode", "eee");
            }

        }catch (Exception e){

        }
    }



//    private <T> T getResultByGet(String url) throws Exception {
//        Request request = new Request.Builder().url(url).build();
//        Response response = httpClient.newCall(request).execute();
//        T rs = null;
//        if (response.code() == 200){
//            Type type = new TypeToken<T>(){}.getType();
//            rs=gson.fromJson(response.body().string(), type);
//            return rs;
//        }
//
//        return rs;
//    }
//
//    private <T> T getResultByGet(Request request, T resultType) throws Exception {
//        Response response = httpClient.newCall(request).execute();
//        T rs = null;
//        if (response.code() == 200){
//            Type type = new TypeToken<T>(){}.getType();
//            rs=gson.fromJson(response.body().string(), type);
//            return rs;
//        }
//
//        return rs;
//    }

}
